<?php
/**
 * Created by PhpStorm.
 * User: DongYao
 * Date: 2018/1/5
 * Time: 下午5:44
 */

namespace XmtApp\Modules\Admin\Controllers\Api;


use App\Http\Resources\OperationLogsResource;
use App\Tables\OperationLog;
use Illuminate\Http\Request;

class OperationLogController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 查询操作日志
     * @param Request $request
     * @return OperationLogsResource
     */
    public function index(Request $request)
    {
        $query = new OperationLog();

        if ($request->filled('level')) {
            $query = $query->where('level', $request->level);
        }

        if ($request->filled('keyword')) {
            $query = $query->where(function ($query) use ($request) {
                $query->orWhere('title', 'like', "%{$request->keyword}%");
                $query->orWhere('content', 'like', "%{$request->keyword}%");
            });
        }

        if ($request->filled('created_at')) {
            $query = $query->whereBetween('created_at', $request->created_at);
        }

        $query = $query->orderBy('id', 'desc');
        $data = $query->paginate($request->header('pageSize'));

        return new OperationLogsResource($data);
    }
}